//
//  DMNetworkTool.m
//  CQKuaiYi
//
//  Created by 何典钊 on 2017/1/8.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMNetworkTool.h"

@implementation DMNetworkTool

- (void)postURL:(NSString *)url parameter:(NSDictionary *)parameters completion:(void (^)(id))completion {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //需要json请求
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //需要设置返回值类型修改
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",nil];
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"responseObject = %@",responseObject);
        completion(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"error = %@",error);
    }];
    
}

+ (id)shareDMNetwork {
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] initWithBaseURL:nil];
    });
    
    return instance;
}
- (void)GETWithURLString:(NSString *)URLString parameters:(id)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // AFN的回调,默认就是在主线程
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}


@end
